// import * as api from '@/api/recruitBatchMngApi';
import cookieUtil from '@/utils/cookie.js';
import router from '@/router/router.js';
/*
 * const httpFn = (api, data) => {
 * 	return new Promise((resolve, reject) => {
 * 		api(data).then(({ data: res }) => {
 * 			if (res.code === '000000') {
 * 				return resolve(res.resultBody);
 * 			}
 * 			reject(res);
 * 		});
 * 	});
 * };
 */


export default {
	namespaced: true,
	state: {
		userInfo: {
			userName: '',
			employeeName: '',
			orgName: '',
			deptName: ''
		},
		menuList: [],
		captchaKey: '',
		headTags: [],
		currentId: '1'
	},
	mutations: {
		// 登录
		login(state, data) {
			state.userInfo = data.userInfo;
			state.menuList = data.menuList;
		},
		// 退出
		logoutStore(state) {
			cookieUtil.delCookie('track_token');
			sessionStorage.clear();
			localStorage.clear();
			state.userInfo = {
				userName: '',
				employeeName: '',
				orgName: '',
				deptName: ''
			};
			state.menuList = [];
			state.headTags = [];
		},
		addTag(state, obj) {
			let hasIndex = state.headTags.findIndex((item) => item.id === obj.id);
			if (state.headTags.length > 20) { // 最多允许20个菜单
				state.headTags.splice(state.headTags.length - 1, 1);
			}
			if (hasIndex === -1) {
				state.headTags.push(obj);
			}
			state.currentId = obj.id;
		},
		removeTag(state, obj) {
			for (let index = 0; index < state.headTags.length; index++) {
				const element = state.headTags[index];
				console.log(element, obj);
				if (element.id === obj.id) {
					state.headTags.splice(index, 1);
				}
			}

			if (state.headTags.length == 0) {
				state.currentId = 0;
				router.push('/');
			} else {
				state.currentId = state.headTags[state.headTags.length - 1].id;
				router.push(state.headTags[state.headTags.length - 1].path);
			}
		},
		setCurrentId(state, currentId) {
			state.currentId = currentId;
		},
	},
	actions: {
	}
};
